package email

import (
	"fmt"
	"github.com/jordan-wright/email"
	"gspeed/bootstrap/global"
	"time"
)

type emailUtils struct{}

var Email emailUtils

/**
  From = "dj <xxx@126.com>" 发送者
  To = []string{"935653229@qq.com"}  接收者
  Cc = []string{"test1@126.com", "test2@126.com"} 抄送人
  Bcc = []string{"secret@126.com"} 秘密抄送人
  Subject = "Awesome web" 邮件标题
  Text = []byte("Text Body is, of course, supported!")  邮件内容
  AttachFile("test.txt")  邮件附件
*/
func (e *emailUtils) SendText(receiver []string, sender string, title string, text string) {

	from := fmt.Sprintf("%s <%s>", sender, global.Config.GetString("email.Username"))
	content := &email.Email{
		From:    from, // 先用变量 拼接好文字，不要直接使用方法，否则报错：expected comma
		To:      receiver,
		Subject: title,
		Text:    []byte(text),
	}
	err := global.Email.Send(content, 5*time.Second)
	if err != nil {
		global.Logger.Fatal(err)
	}
	return
}

func (e *emailUtils) SendHtml(receiver []string, sender string, title string, text string) {

	from := fmt.Sprintf("%s <%s>", sender, global.Config.GetString("email.Username"))
	content := &email.Email{
		From:    from, // 先用变量 拼接好文字，不要直接使用方法，否则报错：expected comma
		To:      receiver,
		Subject: title,
		HTML:    []byte(text),
	}
	err := global.Email.Send(content, 5*time.Second)
	if err != nil {
		global.Logger.Fatal(err)
	}
	return
}
